app.controller('userMonitorCtrl', function ($scope, userMonitorService, $timeout) {
    var lastRreciSize = 0;
    var lastSendSize = 0;
    var getAll = function () {
        userMonitorService.getUserMonitor({
            action: 'getAllMonitor'
        }, function (data) {
            if (data.state == 'SUCCESS') {
                $scope.userMonitors = data.data.list;
            }
            //else {
            //    bootbox.alert("查询失败(" + data.reason + ")");
            //}
        });
        $timeout(function () {
            getAll();
        }, $scope.selectTime);
    };
    $scope.changeTime = function(time){
        $scope.sysTime=time;
    }
    var getAllService = function () {
        userMonitorService.getService({
            action: 'getAllService'
        }, function (data) {
            // AMQS
            $scope.amqsPort = data.AMQS.port;
            $scope.amqsHost = data.AMQS.host;
            $scope.amqsChannel = data.AMQS.channel;
            $scope.amqsState = data.AMQS.state;
            $scope.amqsQm=data.AMQS.qm;
            $scope.amqTitle=data.AMQS.state=='CONNECTED'?'正常':'异常';
            //// MQInfo
            $scope.mqInfoPort = data.MQInfo.port;
            $scope.mqInfoHost = data.MQInfo.host;
            $scope.mqInfoName = data.MQInfo.name;
            $scope.mqInfoState = data.MQInfo.state;
            $scope.mqInfoConnectCount=data.MQInfo.connectCount;
            $scope.mqInfoStartTime=data.MQInfo.startTime;
            $scope.mqInfoTitle=data.MQInfo.state=='Running'?'正常':'异常';
            // GW
            $scope.gwName=data.GW.name==null?"———  ———":data.GW.name;
            $scope.gwIp=data.GW.ip;
            $scope.gwPort = data.GW.port;
            $scope.gwHost = data.GW.host;
            $scope.gwChannel = data.GW.channel;
            $scope.gwState = data.GW.state;
            $scope.gwHealthy=data.GW.healthy;
            $scope.gwMode = data.GW.mode==null?"EXCEPTION":data.GW.mode;
            $scope.gwQm=data.GW.qm;
            $scope.gwTitle=data.GW.healthy==true?'正常':'异常';
        });
        $timeout(function () {
            getAllService();
        }, $scope.sysTime);
    };

    var getMonitorLogs = function () {
        if($scope.logCount == null){
            $scope.logCount = '50';
        }
        userMonitorService.getMonitorLogs({
            action : 'getMonitorLogs',
            logCount : $scope.logCount
        },function(data){
            //展示路由数据表格
            if(data.state == 'SUCCESS'){
                $scope.logs = data.data.list;
            }
            $timeout(function(){
                getMonitorLogs();
            },$scope.logTime);
        });
    };
    $scope.changeLogTime = function (time) {
        $scope.logTime = time;
    };
    $scope.changeLogCount = function (count) {
        $scope.logCount = count;
        getMonitorLogs();
    };

    getMonitorLogs();
    getAll();
    getAllService();
    /**
     *
     * @param 显示Dialog
     */
    $scope.showDialogStatusIp = function (ip) {
        $('#dialogStatusIp').on('show.bs.modal', function () {
            $scope.userMonitorIp =ip;
        });
        $("#dialogStatusIp").modal('show');
    };
    $scope.checkNet = function (cmd,ip,pingNum) {
        $("#stateList").text(" ");
        userMonitorService.getMonitorLogs({
                action: 'checkNet',
                cmd:cmd,
                ip: ip,
                pingNum:pingNum
            }, function (data) {
                //展示路由数据表格
                if(data.healthy){
                    $("#stateList").text(data.message)
                }else
                {
                    $("#stateList").text("服务器响应失败")
                }
            }
        )};

    $scope.setBgColor = function(logLevel){
        if (logLevel == '0') {
            return {
                'background-color':'#7AD57A'
            }
        } else if (logLevel == '1') {
            return {
                'background-color':'#FFFF00'
            }
        } else if (logLevel == '2') {
            return {
                'background-color':'#EE9A00'
            }
        } else {
            return {
                'background-color':'#D9524E'
            }
        }
    };

});
